﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;

namespace DBControl
{
    public class SystemLib
    {
        private string key;
        private string secret;
        private string url;


        public static string appkey
        {
            get 
            {
                return "12167888";
            }
        }

        public static string appsecret
        {
            get
            {
                
                if (ConfigurationManager.AppSettings["SandBox"].ToString().Equals("ON"))
                {
                    return "sandbox3606eac16f68e582b5b5e52ba";
                }
                else
                {
                    return "48a9508d05c289338b5ba139289ba23f";
                }
                
            }
        }

        public static string appurl
        {
            get
            {
                if (ConfigurationManager.AppSettings["SandBox"].ToString().Equals("ON"))
                {
                    return "http://gw.api.tbsandbox.com/router/rest";
                }
                else
                {
                    return "http://gw.api.taobao.com/router/rest";
                }
            }
        }

        #region 数据库密码加密解密

        private SymmetricAlgorithm mobjCryptoService = new RijndaelManaged();
        ///   <summary> 
        ///   解密字符串密码;
        ///   </summary> 
        ///   <param   name= "Source "> 待解密的串 </param> 
        ///   <returns> 经过解密的串 </returns> 
        public string Decrypto(string Source)
        {
            byte[] bytIn = Convert.FromBase64String(Source);
            MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            return sr.ReadToEnd();
        }


        ///   <summary> 
        ///   获得密钥 
        ///   </summary> 
        ///   <returns> 密钥 </returns> 
        private byte[] GetLegalKey()
        {
            string sTemp = "exsky001";
            mobjCryptoService.GenerateKey();
            byte[] bytTemp = mobjCryptoService.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
            {
                sTemp = sTemp.Substring(0, KeyLength);
            }
            else if (sTemp.Length < KeyLength)
            {
                sTemp = sTemp.PadRight(KeyLength, ' ');
            }
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        ///   <summary> 
        ///   获得初始向量IV 
        ///   </summary> 
        ///   <returns> 初试向量IV </returns> 
        private byte[] GetLegalIV()
        {
            string sTemp = "hujk619";
            mobjCryptoService.GenerateIV();
            byte[] bytTemp = mobjCryptoService.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
            {
                sTemp = sTemp.Substring(0, IVLength);
            }
            else if (sTemp.Length < IVLength)
            {
                sTemp = sTemp.PadRight(IVLength, ' ');
            }
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        #endregion


        /// <summary>
        /// 解析回调地址中top_parameters中的值
        /// </summary>
        /// <param name="key">取值关键词</param>
        /// <returns></returns>
        public string GetParameters(string parameters, string key)
        {
            string ret = string.Empty;
            try
            {
                string str = Base64ToString(parameters);
                string[] param = str.Split('&');
                for (int i = 0; i < param.Length; i++)
                {
                    string[] info = param[i].Split('=');
                    if (info[0].ToLower() == key.ToLower())
                    {
                        ret = info[1];
                        break;
                    }
                }
            }
            catch (Exception exp)
            {
                //
            }
            return ret;
        }

        /// <summary>
        /// Base64 解密
        /// </summary>
        /// <param name="str">已加密字符串</param>
        /// <returns>源字符串</returns>
        public string Base64ToString(string str)
        {
            try
            {
                return System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(str));
            }
            catch (Exception)
            {
                return "-1";
                throw;
            }
            
        }

        /// <summary>
        /// Base64 加密
        /// </summary>
        /// <param name="str">需要加密的字符串</param>
        /// <returns>已加密字符串</returns>
        //public string Base64Encoding(string str)
        //{
        //    return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(str));
        //}
    }

}
